package ${packageName}.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import ${packageName}.entity.${ClassName};
import ${packageName}.vo.${ClassName}Param;
import ${packageName}.service.${ClassName}Service;
<#if tplCategory == "parent">
import ${packageName}.vo.${ClassName}VO;
</#if>
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

<#-- 类注释 -->
/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @since ${datetime}
*/
@Tag(name = "${functionName}控制器", description = "${functionName}控制器")
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller {
    @Autowired
    private ${ClassName}Service ${className}Service;

<#if tplCategory == "crud" || tplCategory == "parent">
    @Operation(summary = "查询${functionName}列表", description = "查询${functionName}列表")
    @PostMapping("/pageList")
    public Result<IPage<${ClassName}>> pageList(@Parameter(description = "查询参数") @RequestBody ${ClassName}Param ${className}Param) {
        Page<${ClassName}> page = new Page<>(${className}Param.getPage(), ${className}Param.getPageSize());
        ${ClassName} ${className} = new ${ClassName}();
        BeanUtils.copyProperties(${className}Param, ${className});
        QueryWrapper<${ClassName}> wrapper = QueryWrapperUtil.build(${className});
        IPage<${ClassName}> list = ${className}Service.page(page, wrapper);
        return Result.success(list);
    }
</#if>

<#if tplCategory == "tree">
    @Operation(summary = "查询${functionName}树列表", description = "查询${functionName}树列表")
    @PostMapping("/treeList")
    public Result<List<${ClassName}>> treeList(@Parameter(description = "查询参数") @RequestBody ${ClassName}Param ${className}Param) {
    List<${ClassName}> list = ${className}Service.treeList(${className}Param);
        return Result.success(list);
    }

</#if>
<#if tplCategory == "children">
    @Operation(summary = "查询${functionName}树列表", description = "查询${functionName}树列表")
    @PostMapping("/list")
    public Result<List<${ClassName}>> pageList(@Parameter(description = "查询参数") @RequestBody ${ClassName} ${className}) {
        QueryWrapper<${ClassName}> wrapper = QueryWrapperUtil.build(${className});
        List<${ClassName}> list = ${className}Service.list(wrapper);
        return Result.success(list);
    }
</#if>

    @Operation(summary = "获取${functionName}详细信息", description = "获取${functionName}详细信息")
    @GetMapping(value = "/{${table.pkColumn.javaField}}")
    public Result<${ClassName}> getInfo(@Parameter(description = "${functionName}ID") @PathVariable("${table.pkColumn.javaField}") ${table.pkColumn.javaType} ${table.pkColumn.javaField}) {
        return Result.success(${className}Service.getById(${table.pkColumn.javaField}));
    }
<#if tplCategory == "parent">
    @Operation(summary = "新增${functionName}", description = "新增${functionName}")
    @PostMapping
    public Result<?> save(@Parameter(description = "${functionName}") @RequestBody ${ClassName}VO ${className}VO) {
        ${className}Service.save${BusinessName}(${className}VO);
        return Result.success();
    }

    @Operation(summary = "修改${functionName}", description = "修改${functionName}")
    @PutMapping
    public Result<?> update(@Parameter(description = "${functionName}") @RequestBody ${ClassName}VO ${className}VO) {
        ${className}Service.update${BusinessName}(${className}VO);
        return Result.success();
    }

    @Operation(summary = "删除${functionName}", description = "删除${functionName}")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") ${table.pkColumn.javaType}[] ${table.pkColumn.javaField}s) {
        ${className}Service.remove${BusinessName}(Arrays.asList(${table.pkColumn.javaField}s));
        return Result.success();
    }
<#elseif tplCategory == "crud" || tplCategory == "tree">
    @Operation(summary = "新增${functionName}", description = "新增${functionName}")
    @PostMapping
    public Result<?> save(@Parameter(description = "${functionName}") @RequestBody ${ClassName} ${className}) {
        ${className}Service.save${BusinessName}(${className});
        return Result.success();
    }

    @Operation(summary = "修改${functionName}", description = "修改${functionName}")
    @PutMapping
    public Result<?> update(@Parameter(description = "${functionName}") @RequestBody ${ClassName} ${className}) {
        ${className}Service.update${BusinessName}(${className});
        return Result.success();
    }

    @Operation(summary = "删除${functionName}", description = "删除${functionName}")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") ${table.pkColumn.javaType}[] ${table.pkColumn.javaField}s) {
        return Result.success(${className}Service.removeByIds(Arrays.asList(${table.pkColumn.javaField}s)));
    }
</#if>

<#if tplCategory == "crud" || tplCategory == "parent">
    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<${ClassName}> util = new ExcelUtil<>(${ClassName}.class);
        util.importTemplateExcel(response, "${functionName}");
    }

    @Operation(summary = "导出${functionName}列表", description = "导出${functionName}列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ${ClassName}Param ${className}Param) {
        ${ClassName} ${className} = new ${ClassName}();
        BeanUtils.copyProperties(${className}Param, ${className});
        QueryWrapper<${ClassName}> wrapper = QueryWrapperUtil.build(${className});
        List<${ClassName}> list = ${className}Service.list(wrapper);
        ExcelUtil<${ClassName}> util = new ExcelUtil<>(${ClassName}.class);
        util.exportExcel(response, list, "${functionName}数据");
    }

    @Operation(summary = "导入${functionName}列表", description = "导入${functionName}列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<${ClassName}> util = new ExcelUtil<>(${ClassName}.class);
        List<${ClassName}> ${className}List = util.importExcel(file.getInputStream());
        String message = ${className}Service.importData(${className}List, updateSupport);
        return Result.success(message);
    }
</#if>
}
